﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using QLTTTH.Entity;
using QLTTTH.Config;

namespace QLTTTH.DAO
{
    class TaiKhoanDAO:DAOBase
    {
        public DataSet GetAll()
        {
            return Select("select * from TaiKhoan");
        }

        public TaiKhoan GetById(String username)
        {
            TaiKhoan tk = null;
            SqlConnection conn = null;
            try
            {
                conn = Config.Config.getConnection();
                conn.Open();
                SqlCommand commnad = new SqlCommand("select * from TaiKhoan where username= '" + username+ "'", conn);
                SqlDataReader dataReader = commnad.ExecuteReader();
                if (dataReader.Read())
                {
                    tk = new TaiKhoan();
                    tk.Username = dataReader.GetString(0);
                    tk.Password = dataReader.GetString(1);
                    return tk;
                }
            }
            catch (Exception ex)
            {

            }
            finally
            {
                conn.Close();
                
            }
            return tk;
        }

        public bool ChangePass(TaiKhoan tk, String newPassword)
        {
            String sql = "update TaiKhoan "
                + "set password = '" + MD5Encrypte.Encrypte(newPassword) + "'"
                + " where username= '" + tk.Username + "' and Password = '" + MD5Encrypte.Encrypte(tk.Password) + "'";
            return Update(sql);
        }

        public bool DeleteById(String username)
        {
            return Update("delete from TaiKhoan where username = '" + username+ "'");
        }

        public bool UpdateById(TaiKhoan tk)
        {
            String sql = "update TaiKhoan "
                 + "set password = '" + tk.Password + "'"
                 + " where username= '" + tk.Username+ "'" ;
            return Update(sql);
        }

        public bool Insert(TaiKhoan taiKhoan)
        {
    
            String sql = "INSERT INTO [QLVT].[dbo].[taikhoan]([username],[password],[quyen])"
               + "VALUES('" + taiKhoan.Username+ "','" + taiKhoan.Password + "','" + taiKhoan.Quyen + ")";
            return Update(sql);
        }
    }
}
